/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.rum.sender.command.history;

import cz.muni.fi.rum.sender.command.RapMessage;
import cz.muni.fi.rum.sender.command.RapRequest;
import cz.muni.fi.rum.sender.command.RapResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author pmikulasek
 */
public class CommandHistoryManagerImpl implements CommandHistoryManager {

    private Map<RapRequest, RapResponse> history;
    private List<RapMessage> allMessages;
    private static CommandHistoryManager instance;

    private CommandHistoryManagerImpl() {
        this.history = Collections.synchronizedMap(new HashMap<RapRequest, RapResponse>());
        this.allMessages = Collections.synchronizedList(new LinkedList<RapMessage>());
    }

    public static CommandHistoryManager getHistoryManager() {
        if (instance == null) {
            instance = new CommandHistoryManagerImpl();
        }
        return instance;
    }

    @Override
    public Set<RapRequest> getAllRequest() {
        return Collections.unmodifiableSet(history.keySet());
    }

    @Override
    public List<RapMessage> getAllHistory() {
        return Collections.unmodifiableList(allMessages);
    }

    @Override
    public void add(RapRequest request, RapResponse response) {
        allMessages.add(request);
        allMessages.add(response);
        history.put(request, response);
    }

    @Override
    public RapResponse getResponseByRequestId(Long requestId) throws Exception {
        //TODO ===CommandHistory=== nejaky throws pro neexistujici zaznam.
//        return history.get(requestId).getResponse();
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public RapResponse getResponseByRequest(RapRequest request) throws Exception {
        //TODO ===CommandHistory=== nejaky throws pro neexistujici zaznam.
        return history.get(request);
    }
}
